<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>列表商城</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body,
        html {
            height: 100%;
            width: 100%;
            background: rgb(241, 243, 245);
        }

        #box {
            width: 100%;
            background-color: black;
        }

        a,
        li,
        ul {
            list-style: none;
            text-decoration: none;
        }

        #nav {
            height: 36px;
            width: 1200px;
            color: #ccc;
            margin: auto;
        }

        .nav-ul li {
            line-height: 36px;
            float: left;
            background: url(./imgs/fgx.png) no-repeat right;
            font-size: 12px;
            padding-right: 6px;
            display: block;
            margin-right: 6px;
        }

        .nav-ul li a {
            margin: 0 5px;
            color: #ccc;
            text-decoration: none;
        }

        .nav-ul li a:hover {
            color: #fff;
        }

        .left {
            float: left;
        }

        .rigth {
            float: right;
        }

        .nav-ul-rigth {
            float: right;
        }

        #classification {
            width: 100%;
            background: rgb(255, 255, 255);
        }

        .classification-huawei {
            width: 1200px;
            height: 75px;
            margin: auto;
            /* background-color: #ccc; */
        }

        .logo {
            float: left;
            margin-right: 50px;
        }

        .classification-huawei .logo a {
            width: 100px;
            height: 70px;
            display: block;
        }

        .logo a img {
            height: 100%;
            width: 100%;
        }

        .classification-li ul li {
            line-height: 75px;
            float: left;
        }

        .classification-li ul li a {
            color: black;
            margin-right: 40px;
        }

        .classification-li ul li a:hover {
            color: red;
        }





        #buttom {
            width: 100%;
            background-color: #fff;
        }

        .but-list {
            width: 1200px;
            height: 120px;
            margin: auto;

            border-bottom: 1px solid #ccc;
        }

        .but-list img {
            display: block;
            float: left;
            width: 25px;
            height: 25px;
            margin: auto;
            text-align: center;
            margin-right: 10px;
        }

        .but-list-l {
            width: 210px;
            height: 50px;
            float: left;
            margin-top: 50px;
            margin-left: 90px;
        }

        .but-list .but-list-l:nth-child(1) {
            margin-left: 50px;
        }

        .but-nav {
            width: 1200px;
            margin: auto;
            padding: 50px 0;
        }

        .but-nav-ul {
            float: left;
        }

        .but-nav-ul li {
            margin-top: 10px;
            cursor: pointer;
        }

        .but-nav-box {
            height: 200px;
            font-size: 12px;
            color: #888888;
        }

        .but-nav-ulLi {
            font-size: 14px;
            color: black;
            margin-bottom: 30px;
        }

        .ul-left {
            margin-left: 100px;
        }

        .but-nav-ul li:hover {
            color: red;
        }

        .but-dz {
            width: 250px;
            height: 200px;
            /* background-color: pink; */
            margin-left: 70px;
            border-left: 1px solid #ccc;
        }

        .but-dz p {
            color: black;
            font-size: 24px;
            text-align: center;
            margin-top: 20px;
        }

        .but-dz p:nth-child(2) {
            font-size: 14px;
        }

        .but-dz a {
            display: block;
            width: 200px;
            height: 40px;
            background-color: black;
            color: #fff;
            font-size: 16px;
            text-align: center;
            line-height: 40px;
            border-radius: 30px;
            margin: 19px auto;
        }

        .but-dz img {
            display: block;
            margin: auto;
        }

        #pls {
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .imgs {
            width: 100%;
            height: 550px;
            /* background-color: aqua; */
        }

        #list {
            width: 1200px;
            margin: auto;
            margin-bottom: 20px;
            overflow: hidden
        }

        .box-list {
            height: 550px;
            /* background-color: aqua; */
            text-align: center;
            line-height: 550px;
            display: none;
        }

        #catr-style {
            width: 100%;
            height: 50px;
            background-color: #fff;
            margin: 20px auto;
            font-size: 12px;
            color: #888888;
            /* padding:10px 30px; */
            position: relative;
        }

        .check {
            width: 15px;
            height: 15px;
            position: absolute;
            top: 18px;
            left: 30px;
        }

        #catr-style span {
            line-height: 50px;
            margin-left: 60px;
        }

        .span1 {
            margin-left: 10px;
            margin-right: 70px;
        }

        .span2 {
            margin-right: 350px;
        }

        .span3 {
            margin-right: 70px;
        }

        .span4 {
            margin-right: 70px;
        }

        .span5 {
            margin-right: 70px;
        }
        .catr-list {
            width: 100%;
            height: 125px;
            background-color: #fff;
            overflow: hidden;


        }

        .catr-list .check-list {
            width: 15px;
            height: 15px;
            float: left;
            margin: 60px 30px;
        }

        .catr-list img {
            display: block;
            width: 100px;
            height: 100px;
            margin: 15px 0px;
        }

        .catr-list ul {
            font-size: 14px;
            margin: 15px 0px;
            width: 1000px;
        }

        .catr-list ul li {
            float: left;
            margin-right: 100px;
        }

        .catr-list ul li:nth-child(1) {
            float: left;
            width: 450px;
            margin: 0;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }

        .catr-list ul li:nth-child(3) {
            margin-right: 80px;
        }

        .catr-list ul li:nth-child(4) {
            margin-right: 70px;
        }

        .cart-van {
            color: #3a3a3a;
        }

        .cart-list {
            color: #888888;
            font-size: 12px;
        }

        .pos {
            float: left;
            border: #888888 1px solid;
        }

        .settlement input {
            width: 25px;
            height: 25px;
            /* border: #888888 1px solid; */
            text-align: center;
            font-size: 14px;
            background-color: #fff;
            border: none;

        }

        .settlement button {
            width: 25px;
            height: 25px;
            font-size: 16px;
            /* border: #888888 1px solid; */
            text-align: center;
            background-color: #fff;
            border-left: none;
            border: none;
        }

        .settlement .jia {
            border-left: 1px solid #888;
        }

        .settlement .jian {
            cursor: not-allowed;
            color: #c4c4c4;
            border-right: 1px solid #888;
        }

        #cart-xiadan {
            width: 100%;
            height: 60px;
            background-color: #fff;
            margin-top: 20px;
        }

        .xiadan-red {
            width: 200px;
            height: 60px;
            background-color: red;
            text-align: center;
            font-size: 20px;
            color: #fff;
            line-height: 60px;
        }

        .xiadan-num {
            width: 150px;
            height: 60px;
            /* background-color: aqua; */
            line-height: 30px;
            margin-right: 20px;
        }

        .xiadan-num p {
            float: right;
        }
    </style>
</head>

<body>
    <div id="box">
        <!-- top -->
        <div id="nav">
            <ul class="nav-ul left">
                <li><a href="index.html">首页</a></li>
                <li><a href="">华为官网</a></li>
                <li><a href="">花粉俱乐部</a></li>
                <li><a href="">V码(优购码)</a></li>
                <li><a href="">企业购</a></li>
                <li><a href="">Select Region</a></li>
                <li style="background: url()"><a href="">更多精彩</a></li>
            </ul>
            <ul class="nav-ul right" style="float: right">
                <li><a href="" class="mobile">请登录</a>&nbsp;<a href="" class="mobile1">注册</a></li>
                <li><a href="">我的订单</a></li>
                <li><a href="">客服</a></li>
                <li><a href="">网站导航</a></li>
                <li><a href="">手机版</a></li>
                <li style="background: url(); background-color: rgb(66, 66, 66)">
                    <a href="./cart.html">购物车</a>
                </li>
            </ul>
        </div>
    </div>
    <div id="classification">
        <div class="classification-huawei">
            <div class="logo">
                <a href="">
                    <img src="./imgs/logo.png" alt="" />
                </a>
            </div>
            <div class="classification-li">
                <ul>
                    <li style="font-size: 24px;">我的购物车</li>
                </ul>
                <img src="./imgs/gwc.jpg" alt="" class="rigth" width="300px" style="margin-top: 10px;">
            </div>
        </div>
    </div>
    <!---->
    <!-- <div class="imgs">
            <img src="./imgs/2.webp" alt="限时秒杀" class="imgs-value">
        </div> -->

    <div id="list">
        <div class="box-list">
            <h1>暂无商品</h1>
        </div>
        <div id="catr-style">
            <input type="checkbox" class="check">
            <i> </i>
            <span class="span1">全选</span>
            <span class="span2">商品</span>
            <span class="span3">单价</span>
            <span class="span4">数量</span>
            <span class="span5">小计</span>
            <span class="span6">操作</span>
        </div>
        <div id="catr-res">

            <!------>
            <div class="catr-list">
                <div class="left">
                    <input type="checkbox" class="check-list">
                    <img src="./imgs/tab-shouji7.png" alt="">
                </div>
                <ul class="left">
                    <li>
                        <span class="cart-van">HUAWEI P50E 8GB+256GB 曜金黑</span><br>
                        <span class="cart-list">曜金黑 4G全网通 8GB+256GB 官方标配（无充电套装及耳机）</span>
                    </li>
                    <li>
                        <span>￥4488.00</span>
                    </li>
                    <li class="settlement">
                        <div class="pos">
                            <button class="jian">-</button>
                            <input type="text" value="1">
                            <button class="jia">+</button>
                        </div>
                    </li>
                    <li>
                        <span style="color: red;">￥4488.00</span>
                    </li>
                    <li style="margin: 0;margin-left: 28px;color: #888;">
                        <span>删除</span>
                    </li>
                </ul>
            </div>
            <!------>


        </div>

        <div id="cart-xiadan">

            <div class="xiadan-red rigth">
                立即下单
            </div>
            <div class="xiadan-num rigth">
                <p>总计： <span class="moy" style="color: red;">￥0.00</span></p>
                <p style="color: #888; font-size: 12px;">已选择<span class="num" style="color: red;">0</span>件商品</p>
            </div>
        </div>

    </div>
    </div>

    <div id="buttom">
        <div class="but-list">
            <div class="but-list-l">
                <img src="./imgs/but1 (1).png" alt="" />
                百强企业 品质保证
            </div>
            <div class="but-list-l">
                <img src="./imgs/but2.png" alt="" />
                七天退货 15天换货
            </div>
            <div class="but-list-l">
                <img src="./imgs/but4.png" alt="" />
                48元起免运费
            </div>
            <div class="but-list-l">
                <img src="./imgs/but3.png" alt="" />
                2000家服装店 全国联保
            </div>
        </div>
        <div class="but-nav">
            <div class="but-nav-box">
                <ul class="but-nav-ul">
                    <li class="but-nav-ulLi">购物指南</li>
                    <li>免息分期</li>
                    <li>以旧换新</li>
                    <li>众测活动</li>
                    <li>企业购</li>
                    <li>O2O采购</li>
                </ul>
                <ul class="but-nav-ul ul-left">
                    <li class="but-nav-ulLi">售后服务</li>
                    <li>免息分期</li>
                    <li>以旧换新</li>
                    <li>众测活动</li>
                    <li>企业购</li>
                    <li>O2O采购</li>
                </ul>
                <ul class="but-nav-ul ul-left">
                    <li class="but-nav-ulLi">维修与技术支持</li>
                    <li>免息分期</li>
                    <li>以旧换新</li>
                    <li>众测活动</li>
                    <li>企业购</li>
                    <li>O2O采购</li>
                </ul>
                <ul class="but-nav-ul ul-left">
                    <li class="but-nav-ulLi">特色服务</li>
                    <li>免息分期</li>
                    <li>以旧换新</li>
                    <li>众测活动</li>
                    <li>企业购</li>
                    <li>O2O采购</li>
                </ul>
                <ul class="but-nav-ul ul-left">
                    <li class="but-nav-ulLi">关于我们</li>
                    <li>免息分期</li>
                    <li>以旧换新</li>
                    <li>众测活动</li>
                    <li>企业购</li>
                    <li>O2O采购</li>
                </ul>
                <ul class="but-nav-ul ul-left">
                    <li class="but-nav-ulLi">友情链接</li>
                    <li>免息分期</li>
                    <li>以旧换新</li>
                    <li>众测活动</li>
                    <li>企业购</li>
                    <li>O2O采购</li>
                </ul>
                <div class="but-dz left">
                    <p>950805</p>
                    <p>7 x 24 小时客服热线(仅收市话费)</p>
                    <a href="">在线客服</a>
                    <img src="./imgs/buts.png" alt="" />
                </div>
            </div>
        </div>
    </div>
</body>

</html>
<script>
    function getUrlParam(key) {
        // ?id=2&type=1
        let result = '';
        location.search.substr(1).split('&').forEach(item => {
            let keyValue = item.split('=');
            if (keyValue[0] == key) {
                result = keyValue[1];
            }
        })
        return result;
    }
</script>
<script>
    // console.log(localStorage.getItem("huaweiID"))
    if (!localStorage.getItem("huaweiID")) {
        alert("请先登录")
        location.href = "login.html"
    }
    let mobile = document.querySelector(".mobile")
    let mobile1 = document.querySelector(".mobile1")
    if (localStorage.getItem("huaweiName")) {
        mobile.innerHTML = localStorage.getItem("huaweiName")
        mobile1.innerHTML = "退出账号"
    }
    mobile.onclick = function () {
        console.log(mobile1.innerHTML)
        if (mobile.innerHTML == "请登录") {
            localStorage.setItem('urlS', location.href);
            location.href = "login.html"
            return false
        } else {
            alert("账号名称")
            return false
        }
    }
    mobile1.onclick = function () {
        console.log(mobile1.innerHTML)
        if (mobile1.innerHTML == "退出账号") {
            localStorage.removeItem("huaweiName")
            localStorage.removeItem("huaweiID")
            location.href = "index.html"
            return false
        } else {
            location.href = "register.html"
            return false
        }
    }
    //跳转
    function listSrc(a) {
        // console.log(a.getAttribute("value"));
        if (localStorage.getItem("huaweiName")) {
            location.href = `detail.html?goodsId=${a.getAttribute("value")}`;
            return
        }
        alert("请先登录")
        localStorage.setItem('urlS', location.href);
        location.href = "login.html"
    }

    let xhrx = new XMLHttpRequest();
    xhrx.open("get", "http://phpclub.org.cn:8080/api//cart/lists?userId=" + localStorage.getItem("huaweiID"));
    xhrx.send();

    xhrx.onreadystatechange = function () {
        if (xhrx.readyState == 4 && xhrx.status == 200) {

            let res = JSON.parse(xhrx.responseText)
            // console.log(res.data)
            document.querySelector("#catr-res").innerHTML = ""
            
            for (let i = 0; i < res.data.length; i++) {
                document.querySelector("#catr-res").innerHTML +=
                    `
        <div class="catr-list" index="${res.data[i].id}">
               <div class="left">
                    <input type="checkbox" class="check-list">
                    <img src="${res.data[i].goods.goodsImg}" alt="">
               </div>
                <ul class="left">
                    <li>
                        <span class="cart-van">${res.data[i].goods.goodsBody}</span><br>
                        <span class="cart-list">${res.data[i].goods.goodsName}</span>
                    </li>
                    <li>
                        <span>￥${res.data[i].goods.marketPrice}</span>
                    </li>
                    <li class="settlement">
                        <div class="pos">
                            <button class="jian">-</button>
                            <input type="text" value="${res.data[i].goodsCount}">
                            <button class="jia">+</button>
                        </div>
                    </li>
                    <li>
                        <span  style="color: red;">￥${(res.data[i].goodsCount * res.data[i].goods.marketPrice).toFixed(2)}</span>
                    </li>
                   <li style="margin: 0;margin-left: 28px;color: #888;">
                       <span class="del">删除</span>
                   </li>
                </ul>
            </div>
                        `
            }
            document.querySelectorAll(".del").forEach(itme => {
                itme.onclick = function () {
                    xhrx.open("get", "http://phpclub.org.cn:8080/api/cart/remove?cartId=" + itme.parentElement.parentElement.parentElement.getAttribute("index") + "&userId=" + localStorage.getItem("huaweiID"));
                    xhrx.send();
                    xhrx.onreadystatechange = function () {
                        if (xhrx.readyState == 4 && xhrx.status == 200) {
                           location.reload()
                        }
                    }
                }
            })

            //加减
            let pos = document.querySelectorAll(".pos>input")
            let jia = document.querySelectorAll(".pos>.jia")
            let jian = document.querySelectorAll(".pos>.jian")
            jia.forEach((itme, val) => {
                itme.onclick = function () {
                    pos[val].value++
                    if (pos[val].value <= 1) {
                        jian[val].style.cursor = "not-allowed"
                        jian[val].style.color = "#c4c4c4";
                    } else {
                        jian[val].style.cursor = "pointer"
                        jian[val].style.color = "#000";
                    }
                    changeGood(this)
                }
            })
            jian.forEach((itme, val) => {
                itme.onclick = function () {
                    pos[val].value == 1 ? "" : pos[val].value--
                    if (pos[val].value <= 1) {
                        jian[val].style.cursor = "not-allowed"
                        jian[val].style.color = "#c4c4c4";
                    } else {
                        jian[val].style.cursor = "pointer"
                        jian[val].style.color = "#000";
                    }
                    changeGood(this)
                }
            })

            document.querySelectorAll(".check-list").forEach(function (itme) {
                itme.onclick = function () {
                    let flag = (document.querySelectorAll(".check-list").length == document.querySelectorAll(".check-list:checked").length)
                    document.querySelector(".check").checked = flag
                    gettotal()
                }
            })
            function gettotal() {
                let total = 0
                let tonum = 0
                let select = document.querySelectorAll(".check-list:checked")
                select.forEach(itme => {
                    console.log()
                    total += Number(itme.parentElement.parentElement.children[1].children[3].innerText.slice(1))
                    tonum += Number(itme.parentElement.parentElement.children[1].children[2].children[0].children[1].value)
                })
                document.querySelector(".moy").innerHTML = '￥' + total
                document.querySelector(".num").innerHTML = tonum
            }

            function changeGood(elem) {
                let price = elem.parentElement.parentElement.parentElement.children[1].innerText.slice(1)
                let number = elem.parentElement.children[1].value
                elem.parentElement.parentElement.parentElement.children[3].children[0].innerText = "￥" + (price * number).toFixed(2)
                gettotal()
            }

            document.querySelector(".check").onclick = function () {
                _this = this.checked
                document.querySelectorAll(".check-list").forEach(function (item) {
                    item.checked = _this
                })
                gettotal()
            }

            document.querySelector(".xiadan-red").onclick = function() {
                document.querySelector(".num").innerText==0?alert("你还没选，下啥单啊"):alert("咱没钱，看看就好")
            }
            if(document.querySelectorAll(".del").length==0){
                document.querySelector(".box-list").style.display="block"
                return
            }
        }


    }



</script>